<?php
/**
 * User: krisbuist
 * Date: 1/31/12
 * Time: 12:49 PM
 */


if (!function_exists('parse_youtube_url'))
{
    /**
     *  parse_youtube_url() PHP function
     *  Author: takien
     *  URL: http://takien.com
     *
     * @param  string  $url    URL to be parsed, eg:
     *                            http://youtu.be/zc0s358b3Ys,
     *                            http://www.youtube.com/embed/zc0s358b3Ys
     *                            http://www.youtube.com/watch?v=zc0s358b3Ys
     * @param  string  $return what to return
     *                            - embed, return embed code
     *                            - thumb, return URL to thumbnail image
     *                            - hqthumb, return URL to high quality thumbnail image.
     * @param  string     $width  width of embeded video, default 560
     * @param  string  $height height of embeded video, default 349
     * @param int|string $rel whether embeded video to show related video after play or not.
     * @return mixed|string
     */

    function parse_youtube_url($url, $return = 'embed', $width = '', $height = '', $rel = 0)
    {
        $urls = parse_url($url);

        //expect url is http://youtu.be/abcd, where abcd is video iD
        if ($urls['host'] == 'youtu.be')
        {
            $id = ltrim($urls['path'], '/');
        }
        //expect  url is http://www.youtube.com/embed/abcd
        else if (strpos($urls['path'], 'embed') == 1)
        {
            $id = end(explode('/', $urls['path']));
        }
        //expect url is abcd only
        else if (strpos($url, '/') === false)
        {
            $id = $url;
        }
        //expect url is http://www.youtube.com/watch?v=abcd
        else
        {
            parse_str($urls['query']);
            $id = $v;
        }
        //return embed iframe
        if ($return == 'embed')
        {
            return '<iframe width="' . ($width ? $width : 560) . '" height="' . ($height ? $height : 349) . '" src="http://www.youtube.com/embed/' . $id . '?rel=' . $rel . '" frameborder="0" allowfullscreen>';
        }
        //return normal thumb
        else if ($return == 'thumb')
        {
            return 'http://i1.ytimg.com/vi/' . $id . '/default.jpg';
        }
        //return hqthumb
        else if ($return == 'hqthumb')
        {
            return 'http://i1.ytimg.com/vi/' . $id . '/hqdefault.jpg';
        }
        // else return id
        else
        {
            return $id;
        }
    }
}

if (!function_exists('linkifyYouTubeURLs'))
{
    // Linkify youtube URLs which are not already links.
    function linkifyYouTubeURLs($text)
    {
        $text = preg_replace('~
        # Match non-linked youtube URL in the wild. (Rev:20111012)
        https?://         # Required scheme. Either http or https.
        (?:[0-9A-Z-]+\.)? # Optional subdomain.
        (?:               # Group host alternatives.
          youtu\.be/      # Either youtu.be,
        | youtube\.com    # or youtube.com followed by
          \S*             # Allow anything up to VIDEO_ID,
          [^\w\-\s]       # but char before ID is non-ID char.
        )                 # End host alternatives.
        ([\w\-]{11})      # $1: VIDEO_ID is exactly 11 chars.
        (?=[^\w\-]|$)     # Assert next char is non-ID or EOS.
        (?!               # Assert URL is not pre-linked.
          [?=&+%\w]*      # Allow URL (query) remainder.
          (?:             # Group pre-linked alternatives.
            [\'"][^<>]*>  # Either inside a start tag,
          | </a>          # or inside <a> element text contents.
          )               # End recognized pre-linked alts.
        )                 # End negative lookahead assertion.
        [?=&+%\w]*        # Consume any URL (query) remainder.
        ~ix',
            '<a href="http://www.youtube.com/watch?v=$1">YouTube link: $1</a>',
            $text);
        return $text;
    }
}